package oa.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;

import oa.entity.Company;
import oa.service.CompanyService;
import oa.service.impl.CompanyServiceImpl;

/**
 * Servlet implementation class companyservlet
 */
@WebServlet("/selectcompany")
public class CompanyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CompanyServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		CompanyService company = new CompanyServiceImpl();
		boolean falg = false;
		boolean yanzheng = false;	
		// 查看公司验证
		String companyverify = request.getParameter("selectcompan");
		String companyid = request.getParameter("companyid");

		if ("companyselect".equals(companyverify)) {
			try {
				session.setAttribute("companyidlist", company.selectCompany(Integer.parseInt(companyid)));
				request.getRequestDispatcher("Select-Show/company_tabulation.jsp").forward(request, response);
				;
			} catch (Exception e) {
				// TODO: handle exception
			}
		//判断添加公司
		} else if ("companyadd".equals(companyverify)) {
			
			Company com = null;
			try {
				String companyname = request.getParameter("val-name");
				String companyusname = request.getParameter("val-username");
				String companytime = request.getParameter("val-date");
				Date date = new SimpleDateFormat("yyyy-MM-dd").parse(companytime);
				String companynature = request.getParameter("val-skill");
				String companyscope = request.getParameter("val-scope");
				String companybackground = request.getParameter("val-background");
				String companyculture = request.getParameter("val-culture");
				String companyrange = request.getParameter("val-range");
				String companyrule = request.getParameter("val-rule");
				com = new Company(companyname,date,companyusname,companynature,companyscope,companybackground,companyculture,companyrange,companyrule);
	            falg = company.addCompany(com);	           
	          
				
	            out.print(JSON.toJSONString(falg));
			} catch (Exception e) {
				// TODO: handle exception
			}
		//点击更新时先获取到该公司信息	
		}else if("companyup".equals(companyverify)) {
			 List<Company> companylist = new ArrayList<Company>();
			 try {
				 companylist = company.selectCompany(Integer.parseInt(companyid));
					if(companylist.size()>0) {
						session.setAttribute("companyuplist",  companylist);
						request.getRequestDispatcher("company_modify.jsp").forward(request, response);
					}
				
			
			 } catch (Exception e) {
				// TODO: handle exception
			}
			
	    //判断更新	 
		}else if("companyupdate".equals(companyverify)) {
			Company com = new Company();
			try {
				int companygsid = Integer.parseInt(request.getParameter("val-companyid"));
				String companyname = request.getParameter("val-name");
				String companyusname = request.getParameter("val-username");
				String companytime = request.getParameter("val-date");
				Date date = new SimpleDateFormat("yyyy-MM-dd").parse(companytime);
				String companynature = request.getParameter("val-skill");
				String companyscope = request.getParameter("val-scope");
				String companybackground = request.getParameter("val-background");
				String companyculture = request.getParameter("val-culture");
				String companyrange = request.getParameter("val-range");
				String companyrule = request.getParameter("val-rule");
				com.setCompanyId(companygsid);
	            com.setCompanyName(companyname);
	            com.setCompanyTime(date);
	            com.setCompanyUsername(companyusname);
	            com.setCompanyNature(companynature);
	            com.setCompanyScope(companyscope);
	            com.setCompanyBackground(companybackground);
	            com.setCompanyCulture(companyculture);
	            com.setCompanyRange(companyrange);
	            com.setCompanyRule(companyrule);	      
				falg = company.updateCompany(com);	           
	            
			} catch (Exception e) {
				// TODO: handle exception
			}
			out.print(JSON.toJSONString(falg));
        //判断删除
		}else if("selectdel".equals(companyverify)) {
			String companydelid = request.getParameter("companyid");			
			try {
				falg=company.deleteCompany(Integer.parseInt(companydelid));
				
				
				out.print(JSON.toJSONString(falg));
			} catch (Exception e) {
				// TODO: handle exception
			}
			
			
		}
		
		out.flush();
		out.close();

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
